"use strict";
	// 百度地图API功能
	function G(id) {
		return document.getElementById(id);
	}

  var map = new BMap.Map("allmap");
  var point =new BMap.Point(118.725693,32.144815)
  map.centerAndZoom(point, 12);
  var marker = new BMap.Marker(point);  // 创建标注
  map.addOverlay(marker);               // 将标注添加到地图中
  map.panBy(305,165);
  var opts = {
	  position : point,    // 指定文本标注所在的地理位置
	  offset   : new BMap.Size(0, 0)    //设置文本偏移量
	}
  var label = new BMap.Label("江苏宝铁龙运输汽车有限公司", opts); 
  label.setStyle({
    color : "#000000",
    fontSize : "12px",
    height : "20px",
    lineHeight : "20px",
    fontFamily:"微软雅黑"
  });
map.addOverlay(label); 
	var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
		{"input" : "suggestId"
		,"location" : map
	});

	map.addControl(new BMap.NavigationControl());               // 添加平移缩放控件
	map.addControl(new BMap.MapTypeControl());          //添加地图类型控件  
  
	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
	var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
		
		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
		G("searchResultPanel").innerHTML = str;
	});

	var myValue;
	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
	var _value = e.item.value;
		myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
		
		setPlace();
  });

  $('.item button').on('click',function(){
    var _value = $('#suggestId').val();
    var local = new BMap.LocalSearch(map, {
      renderOptions:{map: map}
    });
    local.search(_value);
  })

	function setPlace(){
		map.clearOverlays();    //清除地图上所有覆盖物
		function myFun(){
      console.log( local.getResults())
			var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
			map.centerAndZoom(pp, 18);
			map.addOverlay(new BMap.Marker(pp));    //添加标注
		}
		var local = new BMap.LocalSearch(map, { //智能搜索
		  onSearchComplete: myFun
		});
		local.search(myValue);
	}
